<template>
	<div class="poster_list table-theme">
		<head-title title="营销--海报列表"></head-title>
		<el-card>
			<div class="top-menu">
				<div class="menu-left">
					<select-view
						:option-list="statusList"
						:value="status"
						placeholder="类型"
						type="status"
						@value="setSelectValue"
					>
					</select-view>
				</div>

				<div class="menu-right">
					<refresh-button @refresh="refreshPage"></refresh-button>
					<div class="add">
						<el-button type="primary" @click="data(1)">添加海报</el-button>
					</div>
				</div>
			</div>
			<p class="data-number">总数据数：{{total}}</p>
			<el-table :data="bannerList" border>
				<el-table-column label="序号" align="center" prop="poster_id"></el-table-column>
				<el-table-column label="图片" align="center" width="80">
					<template slot-scope="scope">
						<ImgPopover :src="scope.row.image"></ImgPopover>
					</template>
				</el-table-column>
				<el-table-column label="用户ID" align="center" prop="user_id"></el-table-column>
				<el-table-column label="店铺ID" align="center" prop="store_id"></el-table-column>
				<el-table-column label="店铺名" align="center" prop="store_name"></el-table-column>
				<el-table-column label="排序" align="center" prop="" width="180">
					<template slot-scope="scope">
						<el-tooltip class="item" effect="dark" content="点击修改排序" placement="bottom">
							<div class="sort-input-view" @click.stop="setSort(scope.row.poster_id, scope.row.sort)">
								<el-input v-if="sortid == scope.row.poster_id" v-model="sortForm.sort" placeholder="" type="number" onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))">
									<el-button slot="append" icon="el-icon-check" @click.stop="postSort(0)"></el-button>
									<el-button slot="append" icon="el-icon-close" @click.stop="postSort(1)"></el-button>
								</el-input>
								<span v-else>{{ scope.row.sort }}</span>
							</div>
						</el-tooltip>
					</template>
				</el-table-column>
				<el-table-column label="上下架" align="center" width="180">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" @change="upDown($event, scope.row.poster_id)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="添加时间" align="center" prop="add_time"></el-table-column>
				<el-table-column label="操作" align="center" width="150">
					<template slot-scope="scope">
						<el-button type="primary" size="mini" @click="data(0, scope.row.poster_id)">修改</el-button>
						<el-button type="danger" size="mini" @click="del(scope.row.poster_id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="pages">
				<el-pagination background layout="prev, pager, next, jumper" :total="total" @current-change="init" :current-page="page"></el-pagination>
			</div>
		</el-card>
	</div>
</template>

<script>
	import ImgPopover from '@/components/ImgPopover';
	import SelectView from "@/components/SelectView";
	export default {
		components: {
			ImgPopover,
			SelectView
		},
		data() {
			return {
				sortNum: '',
				sortid: '',
				sortForm: {
					sort: '',
					poster_id: ''
				},
				bannerList: [],
				total: 0,
				totalData: 0,
				status: 1,
				client: '',
				statusList: [{
						value: 1,
						label: '上架'
					},
					{
						value: 2,
						label: '下架'
					}
				],
				page: 0
			};
		},
		watch: {
			'sortForm.sort': function(val) {
				if (val < 0) {
					this.sortForm.sort = 0;
					this.$message.error('请输入正确排序');
				}
			}
		},
		methods: {
			// 初始化列表
			init(e) {
				this.page = e;
				let data = {
					page: e - 1 ? e - 1 : 0,
					limit: 10,
					status: this.status
				};
				this.$http(this.$ApiList.PosterList, data).then(res => {
					this.bannerList = res.datas.data;
					this.total = res.datas.count;
					this.totalData = res.datas.count;

				});
			},
			// 上下架
			upDown(e, poster_id) {
				let data = {
					status: e,
					poster_id: poster_id
				};
				//console.log(data)
				this.$http(this.$ApiList.PosterDown, data).then(res => {
					this.$layer.msg('操作成功');
				});
			},
			setSort(id, sort) {
				this.sortNum = sort;
				this.sortid = id;
				this.sortForm.sort = sort;
				this.sortForm.poster_id = id;
			},
			postSort(type) {
				//  0 确定 1取消
				if (type == 0) {
					if (this.sortNum == this.sortForm.sort) {
						this.sortid = '';
						return;
					}
					this.$http(this.$ApiList.PosterUp, this.sortForm).then(res => {
						this.sortid = '';
						this.init();
					});
				} else {
					this.sortid = '';
				}
			},
			// 删除
			del(poster_id) {
				this.$confirm('确定删除吗?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						let data = {
							poster_id: poster_id
						};
						this.$http(this.$ApiList.DelPoster, data).then(res => {
							this.$layer.msg('删除成功');
							this.page = 1;
							this.init();
						});
					})
					.catch(() => {});
			},
			// 修改或添加
			data(isAdd, id) {
				if (isAdd == 1) {
					this.$router.push({
						path: '/poster_data',
						query: {
							isAdd: isAdd
						}
					});
				} else {
					this.$router.push({
						path: '/poster_data',
						query: {
							isAdd: isAdd,
							id: id
						}
					});
				}
			},
			sort() {
				this.init();
			},
			refreshPage() {
				this.page = 1;
				this.init()
			},
			setSelectValue(obj){
				const{ type,value } = obj
				switch (type) {
					case 'status':
						this.status = value
						break;
				
					default:
						break;
				}
				this.init()
			}
		},
		created() {
			this.init();
		}
	};
</script>
<style lang="scss" scoped>
	.poster_list{
		
	}
</style>
